package com.yl.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.yl.common.constant.HttpStatus;
import com.yl.common.core.domain.AjaxResult;
import com.yl.common.core.page.TableDataInfo;
import com.yl.domain.ConferenceApplyDO;
import com.yl.domain.ConferenceDO;
import com.yl.domain.StudentReplyDO;
import com.yl.domain.VideoUrlDO;
import com.yl.domain.dto.StudentDTO;
import com.yl.mapper.ConferenceApplyMapper;
import com.yl.mapper.ConferenceMapper;
import com.yl.mapper.StudentReplyMapper;
import com.yl.mapper.VideoUrlMapper;
import com.yl.service.ConferenceService;
import com.yl.service.TopicService;
import com.yl.service.ZhiXingService;
import com.yl.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ConferenceServiceImpl implements ConferenceService {

    private static final Logger log = LoggerFactory.getLogger(ConferenceServiceImpl.class);

    @Resource
    ConferenceMapper conferenceMapper;
    @Resource
    ConferenceApplyMapper conferenceApplyMapper;
    @Resource
    TopicService topicService;
    @Resource
    VideoUrlMapper urlMapper;

    @Resource
    ZhiXingService zhiXingService;
    @Resource
    StudentReplyMapper studentReplyMapper;

    /**
     * 创建无领导小组会议
     * 1.创建主题 获取主题ID
     * 2.根据主题id创建题目
     * 3.把返回的信息存到本地
     *
     * @param conferenceDO
     * @author : y
     * @date :  2024/6/19 10:41
     */
    @Override
    public AjaxResult save(ConferenceDO conferenceDO) {
        Long conferenceId = null;
        List<ConferenceApplyDO> applyList = conferenceDO.getConferenceApplyList();
        if (applyList == null || applyList.size() == 0) {
            return AjaxResult.error("讨论成员为空。");
        }
        if (conferenceDO.getTopicId() == null) {
            return AjaxResult.error("请先选择本次讨论的主题");
        }
        if (conferenceDO.getConferenceId() == null) {
            conferenceDO.setCreateTime(new Date());
            conferenceMapper.insert(conferenceDO);
        } else {
            // 删除已关联的参会人员
            conferenceApplyMapper.delete(Wrappers.<ConferenceApplyDO>lambdaQuery().eq(ConferenceApplyDO::getConferenceId, conferenceId));
            conferenceMapper.updateById(conferenceDO);
        }
        conferenceId = conferenceDO.getConferenceId();
        // 开始添加参会人员
        for (ConferenceApplyDO applyDO : applyList) {
            applyDO.setConferenceId(conferenceId);
            applyDO.setCreateTime(new Date());
            //TODO  添加当前账号创建人信息
            conferenceApplyMapper.insert(applyDO);
        }
        return AjaxResult.success("操作成功");
    }

    /**
     * 修改每个阶段时长和每名学生发言时长
     *
     * @param conferenceDO
     * @author : y
     * @date :  2024/6/19 10:43
     */
    @Override
    public AjaxResult update(ConferenceDO conferenceDO) {
        conferenceMapper.updateById(conferenceDO);
        return AjaxResult.success("操作成功");
    }

    /**
     * 取消会议
     *
     * @param conferenceDO
     * @author : y
     * @date :  2024/6/19 10:44
     */
    @Override
    public AjaxResult cancel(ConferenceDO conferenceDO) {
        if (conferenceDO.getConferenceId() == null) {
            return AjaxResult.error("id为空");
        }
        ConferenceDO byId = conferenceMapper.selectById(conferenceDO.getConferenceId());
        if (byId == null) {
            return AjaxResult.error("未查询到会议信息");
        }
        byId.setConferenceStatus(conferenceDO.getConferenceStatus());
        conferenceMapper.updateById(byId);
        return AjaxResult.success("取消成功");
    }

    public void fillConference(ConferenceDO conferenceDO) {
        // 1.查找参会人员信息
        List<ConferenceApplyDO> list = conferenceApplyMapper.selectList(Wrappers.<ConferenceApplyDO>lambdaQuery().eq(ConferenceApplyDO::getConferenceId, conferenceDO.getConferenceId()));
        if (list != null && list.size() != 0) {
            conferenceDO.setConferenceApplyList(list);
            // 所有人的回答结果
            List<StudentReplyDO> doList = studentReplyMapper.findListByConferenceId(conferenceDO.getConferenceId());
            if (doList != null && doList.size() != 0) {
                conferenceDO.setStudentReplyDOList(doList);
            }else{
                conferenceDO.setStudentReplyDOList(new ArrayList<>());
            }

        }
    }

    /**
     * 查看会议列表
     *
     * @param conferenceDO
     * @author : y
     * @date :  2024/6/19 10:44
     */
    @Override
    public TableDataInfo findPage(ConferenceDO conferenceDO) {
        TableDataInfo info = CommonUtils.getTableDataInfo();
        List<ConferenceDO> page = conferenceMapper.findPage(conferenceDO);
        if (page != null && page.size() != 0) {
            for (ConferenceDO aDo : page) {
                fillConference(aDo);
            }
            info.setTotal(new PageInfo<>(page).getTotal());
            info.setRows(page);
        }
        return info;
    }

    /**
     * 查看会议详情
     *
     * @param conferenceDO
     * @author : y
     * @date :  2024/6/19 10:49
     */
    @Override
    public AjaxResult getConferenceDetail(ConferenceDO conferenceDO) {
        List<ConferenceDO> page = conferenceMapper.findPage(conferenceDO);
        if (page != null && page.size() != 0) {
            ConferenceDO conferenceDO1 = page.get(0);
            fillConference(conferenceDO1);
            // 查询主题详情
            AjaxResult topicById = topicService.getTopicById(conferenceDO1.getTopicId());
            try {
                Map<String, Object> toMap = CommonUtils.objectToMap(topicById.get("data"));
                conferenceDO1.setTopicDetail(toMap);
                // 获取视频地址
                List<VideoUrlDO> videoUrlDOS = urlMapper.selectList(Wrappers.<VideoUrlDO>lambdaQuery().orderByAsc(VideoUrlDO::getVideoUrlId));
                if (videoUrlDOS != null && videoUrlDOS.size() != 0) {
                    conferenceDO1.setCommonVideoUrlList(videoUrlDOS);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return AjaxResult.success(conferenceDO1);
        }
        return AjaxResult.success(new ConferenceDO());
    }

    /**
     * 会议结束
     * 1. 修改会议状态
     * 2. 给小组成员创建面试会议 获取session_id 并且保存下来
     *
     * @param conferenceDO
     * @author : y
     * @date :  2024/7/14 11:38
     */
    @Override
    public AjaxResult endConference(ConferenceDO conferenceDO) {
        if (conferenceDO.getConferenceId() == null) {
            return AjaxResult.error("");
        }


        return null;
    }
}
